package com.ruoyi.common.utils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.framework.redis.RedisCache;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.URLEncoder;
import java.util.concurrent.TimeUnit;

/**
 * 视频对接公共类
 */
public class VideoUtils {


    private static final Logger logger = LoggerFactory.getLogger(VideoUtils.class);

    private static RedisCache redisCache = SpringUtils.getBean(RedisCache.class);

    // 新区登录接口
    private static String xq_login_url = "http://61.177.139.223:8787/mgr/login";
    // 新区直播接口
    private static String xq_live_url = "http://61.177.139.223:8787/mgr/bus/live";
    // 新区查询接口
    private static String xq_query_url = "http://61.177.139.223:8787/mgr/bus/queryRecord";
    // 新区下载接口
    private static String xq_download_url = "http://61.177.139.223:8787/mgr/bus/downloadRecord";

    // 锡山直播接口
    private static String xs_live_url = "http://218.90.184.182:27025/ctm01vehdatadock-web/video/preview";
    // 锡山录播接口
    private static String xs_back_url = "http://218.90.184.182:27025/ctm01vehdatadock-web/video/playBack";

    // 宜兴实时接口地址
    public static String yxLiveUrl = "http://61.160.110.130:7202/api/live";
    // 宜兴录播接口地址
    public static String yxPlaybackUrl = "http://61.160.110.130:7202/api/playback";

    /**
     * 新区登录接口
     * @return
     */
    public static String loginXQ(){
        JSONObject str = new JSONObject();
        str.put("username", "admin");
        str.put("password", "111111");
        String json = HttpClientUtil.doPostJson(xq_login_url, str.toString(), null);
        JSONObject object = JSON.parseObject(json);
        if(object.getInteger("code")==20000){
            JSONObject data = object.getJSONObject("data");
            String token = data.getString("token");
            redisCache.setCacheObject("xq_token", token, 2, TimeUnit.HOURS);
            return token;
        }
        logger.error("新区移动登录接口失败，返回值:"+json);
        return null;
    }

    public static String queryRecordXq(String deviceId, String startTime, String endTime) throws Exception{
        String url = xq_query_url + "?deviceId="+deviceId+"&startTime="+ URLEncoder.encode(startTime, "utf-8")+"&endTime="+URLEncoder.encode(endTime, "utf-8");
        // 获取token
        String token = "";
        if(redisCache.getCacheObject("xq_token") == null){
            token = loginXQ();
        }else{
            token = redisCache.getCacheObject("xq_token");
        }
        return HttpClientUtil.doGet(url, token);
    }

    /**
     * 新区视频直播地址
     * @param deviceId
     * @return
     * @throws Exception
     */
    public static String liveXq(String deviceId) throws Exception{
        String url = xq_live_url + "?deviceId="+deviceId;
        // 获取token
        String token = "";
        if(redisCache.getCacheObject("xq_token") == null){
            token = loginXQ();
        }else{
            token = redisCache.getCacheObject("xq_token");
        }
        return HttpClientUtil.doGet(url, token);
    }

    public static String liveXs(String cameraCode) throws Exception{
        String url = xs_live_url + "?cameraIndexCode="+cameraCode;
        return HttpClientUtil.doGet(url, null);
    }

    public static String playBackXs(String cameraCode, String startTime, String endTime) throws Exception{
        String url = xs_back_url + "?cameraCode="+cameraCode+"&startTime="+URLEncoder.encode(startTime, "utf-8")+"&endTime="+URLEncoder.encode(endTime, "utf-8");
        return HttpClientUtil.doGet(url, null);
    }

    public static String yiLiveVideo(String stationCode){
        String url = yxLiveUrl + "?stationCode="+stationCode;
        return HttpClientUtil.doGet(url, null);
    }

    public static String yxPlaybackVideo(String cameraCode, String startTime, String endTime) throws Exception{
        String url = yxPlaybackUrl + "?cameraCode="+cameraCode+"&startTime="+URLEncoder.encode(startTime, "utf-8")+"&endTime="+URLEncoder.encode(endTime, "utf-8");
        return HttpClientUtil.doGet(url, null);
    }

}
